package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.Update;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.UmsRole;
import com.itheima.mapper.UmsRoleMapper;
import com.itheima.service.db.UmsRoleService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    UmsRoleMapper umsRoleMapper;

    @Override
    public UmsRole findById(Long roleId) {
        return umsRoleMapper.selectById(roleId);
    }


    @Override
    public PageBeanVo findByPage(String keyword, Integer pageNum, Integer pageSize) {
        Page<UmsRole> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, UmsRole::getName, keyword);
        Page<UmsRole> umsRolePage = umsRoleMapper.selectPage(page, lqw);
        return new PageBeanVo(pageNum, pageSize, umsRolePage.getTotal(), umsRolePage.getRecords());
    }

    @Override
    public void create(UmsRole umsRole) {
        umsRoleMapper.insert(umsRole);
    }

    @Override
    public void update(UmsRole umsRole) {
        umsRoleMapper.updateById(umsRole);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<UmsRole> luw = new LambdaUpdateWrapper<>();
        luw.eq(UmsRole::getId, id).set(UmsRole::getStatus, status);
        umsRoleMapper.update(null, luw);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        umsRoleMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<UmsRole> listAll() {
        return umsRoleMapper.selectList(null);
    }
}
